package com.titar.kslikeview.widigets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.titar.kslikeview.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class KsLikeView extends RelativeLayout {
    private final String TAG = KsLikeView.class.getSimpleName();
    private List<Integer> mLikeDrawables;
    private LayoutParams mLayoutParams;

    private KsPathAnimator ksPathAnimator;

    public KsLikeView(Context context) {
        this(context, null);
    }

    public KsLikeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public KsLikeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.KsLikeView);
        int defaultGavor = typedArray.getResourceId(R.styleable.KsLikeView_ksg_default_image, -1);
        int enterDuration = typedArray.getResourceId(R.styleable.KsLikeView_ksg_enter_duration, 1500);
        int curveDuration = typedArray.getResourceId(R.styleable.KsLikeView_ksg_curve_duration, 4500);
        typedArray.recycle();

        init(defaultGavor, enterDuration, curveDuration);
    }

    private void init(int defaultGavor, int enterDuration, int curveDuration) {
        this.mLikeDrawables = new ArrayList<>();
        if (defaultGavor == -1) {
            defaultGavor = R.drawable.love1;
        }

        //获取图片资源
        Drawable drawable = new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), defaultGavor));

        //获取图片宽高
        int picWidth = drawable.getIntrinsicWidth();
        int picHeight = drawable.getIntrinsicHeight();

        //初始化布局参数
        this.mLayoutParams = new RelativeLayout.LayoutParams(picWidth, picHeight);
        this.mLayoutParams.addRule(CENTER_HORIZONTAL);
        this.mLayoutParams.addRule(ALIGN_PARENT_BOTTOM);

        //初始化飘心动画路径
        this.ksPathAnimator = new KsPathAnimator(enterDuration,curveDuration);
        this.ksPathAnimator.setPic(picWidth, picHeight);
    }

    public void addLikeImage(Integer resIds) {
        this.mLikeDrawables.add(resIds);
    }

    public void addLikeImages(Integer[] resIds) {
        this.mLikeDrawables.addAll(Arrays.asList(resIds));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        this.ksPathAnimator.setView(getWidth(), getHeight());
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.ksPathAnimator.setView(getWidth(), getHeight());
    }

    /**
     * 添加一个飘心
     */
    public void addFavor() {
        ImageView favor = new ImageView(getContext());
        int position = Math.abs(ksPathAnimator.mRandom.nextInt(mLikeDrawables.size()));
        favor.setImageResource(mLikeDrawables.get(position));
        this.ksPathAnimator.start(favor, this, mLayoutParams);
    }
}
